<template>
	<view>
		<j-cell-group v-if="pid === constant.DEVICE_TYPE.EE_DEV_BOUTIQUEROBOT" title="移动追踪">
			<j-cell title="移动追踪开关" :more="false">
				<view slot="tips">
					<!-- <switch color="#FF0025" :checked="TrackingShow" @change="bindchangeTracking"></switch> -->
					<j-switch v-model="TrackingShow" @change="bindchangeTracking"></j-switch>
				</view>
			</j-cell>
			<j-cell-card title="移动追踪灵敏度" v-if="TrackingShow" :show="false">
				<template v-for="(item, index) in levels">
					<j-cell :key="index" :title="item.name" :more="false" @jftap="bindchangeSensitivity(item)">
						<view slot="tips" v-if="Sensitivity === item.value">
							<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025">
							</uni-icons>
						</view>
					</j-cell>
				</template>
			</j-cell-card>
		</j-cell-group>
		<j-cell-group title="其他设置">
			<j-cell title="图像增强" v-if="imgEnhanceshow" :more="false">
				<view slot="tips">
					<switch color="#FF0025" :checked="imgEnhance" @change="imgEnhanceChange"></switch>
				</view>
			</j-cell>
			<j-cell-card v-if="pid === constant.DEVICE_TYPE.EE_DEV_BOUTIQUEROBOT" title="H265X" :show="false" :tips="h264?'h264':h264plus?'h264+':h265X?'h265X':''"
				:is-touch="false" border style="background: #f5f7fb">
				<j-cell title="H264" :more="false" @jftap="bindtapRecordMode('h264')">
					<view slot="tips" v-if="h264">
						<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025"></uni-icons>
					</view>
				</j-cell>
				<j-cell title="H264+" :more="false" @jftap="bindtapRecordMode('h264plus')">
					<view slot="tips" v-if="h264plus">
						<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025"></uni-icons>
					</view>
				</j-cell>
				<j-cell title="H265X" :more="false" @jftap="bindtapRecordMode('h265X')">
					<view slot="tips" v-if="h265X">
						<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025"></uni-icons>
					</view>
				</j-cell>
			</j-cell-card>
		</j-cell-group>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				device: {},
				h264: false,
				h264plus: false,
				h265X: false,
				TrackingShow: false,
				format: '',
				levels: [{
						value: 0,
						name: '低'
					},
					{
						value: 1,
						name: '中'
					},
					{
						value: 2,
						name: '高'
					}
				],
				Sensitivity: 0,
				imgEnhance: false,
				imgEnhanceshow: false,
				ability: '',
				pid:'',
				constant:getApp().globalData.constant
			}
		},
		onLoad(options) {
			this.device = options
			this.pid = options.pid
			this.ability = JSON.parse(options.ability)
			let ability = JSON.parse(options.ability)
			if (ability.EncodeFunction.SmartH264 && !ability.EncodeFunction.SmartH264V2) {
				this.imgEnhanceshow = true
				this.getconfig('AVEnc.SmartH264')
			}
			this.getconfig('AVEnc.SmartH264V2');
			if(this.pid === this.constant.DEVICE_TYPE.EE_DEV_BOUTIQUEROBOT){
				this.getconfig('Detect.DetectTrack')
			}
			this.getconfig('Detect.HumanDetection')
		},
		methods: {
			getconfig(v) {
				uni.showLoading({
					mask: true
				});
				let token = this.device.token;
				const data = {
					Name: v
				};
				getApp().globalData.api.getconfig(data, token, (result) => {
					if (v == 'AVEnc.SmartH264V2') {
						let arr1 = result['AVEnc.SmartH264V2'][0].Smart264PlusV2
						let arr2 = result['AVEnc.SmartH264V2'][0].Smart264V2
						this.h264 = arr1[0].SmartH264Plus == 0 && arr1[1].SmartH264Plus == 1 && !arr2[0]
							.SmartH264 &&
							arr2[1].SmartH264
						this.h264plus = arr1[0].SmartH264Plus == 0 && arr1[1].SmartH264Plus == 1 && arr2[0]
							.SmartH264 &&
							arr2[1].SmartH264
						this.h265X = arr1[0].SmartH264Plus == 1 && arr1[1].SmartH264Plus == 1 && arr2[0]
							.SmartH264 &&
							arr2[1].SmartH264
					}
					if (v == 'Detect.HumanDetection') {
						this.HumanDetectionEnable = result['Detect.HumanDetection'][0].Enable
					}
					if (v == 'Detect.DetectTrack') {
						console.log(result);
						this.TrackingShow = result['Detect.DetectTrack'].Enable;
						this.Sensitivity = result['Detect.DetectTrack'].Sensitivity
					}
					if (v == 'AVEnc.SmartH264') {
						console.log(result);
						this.imgEnhance = !result['AVEnc.SmartH264'][0].SmartH264;
					}
					setTimeout(res => {
						uni.hideLoading();
					}, 500)
				});
			},
			// 移动灵敏度
			bindchangeSensitivity(ev) {
				this.Sensitivity = ev.value
				this.setconfig('Detect.DetectTrack')
			},
			bindtapRecordMode(v) {
				uni.showLoading();
				this.format = v
				if (v == 'h264') {
					this.h264 = true
					this.h264plus = false
					this.h265X = false
				} else if (v == 'h264plus') {
					this.h264 = false
					this.h264plus = true
					this.h265X = false
				} else if (v == 'h265X') {
					this.h264 = false
					this.h264plus = false
					this.h265X = true
				}
				this.setconfig('AVEnc.SmartH264V2')
			},
			imgEnhanceChange(v) {
				console.log(v)
				this.imgEnhance = v.detail.value;
				this.setconfig('AVEnc.SmartH264')
			},
			bindchangeTracking(v) {
				if (v && this.HumanDetectionEnable) {
					uni.showModal({
						title: '提示',
						content: `人形检测和移动追踪不能同时打开，如果开启移动追踪，人形检测将会被关闭，确认操作？`,
						success: (res) => {
							if (res.confirm) {
								this.HumanDetectionEnable = false
								this.TrackingShow = true;
								this.getconfig('Detect.HumanDetection')
								this.setconfig('Detect.DetectTrack')
							} else if (res.cancel) {
								this.TrackingShow = false
							}
						}
					})
				} else if (v && !this.HumanDetectionEnable) {
					this.TrackingShow = true;
					this.setconfig('Detect.DetectTrack')
				} else {
					this.setconfig('Detect.DetectTrack')
					this.TrackingShow = false
				}
			},
			setconfig(v) {
				let token = this.device.token;
				let data = {
					Name: 'AVEnc.SmartH264V2',
					'AVEnc.SmartH264V2': [{
						Smart264PlusV2: [{
							SmartH264Plus: this.h264 ? 0 : this.h264plus ? 0 : this.h265X ? 1 : null
						}, {
							SmartH264Plus: this.h264 ? 1 : this.h264plus ? 1 : this.h265X ? 1 : null
						}],
						Smart264V2: [{
								SmartH264: this.h264 ? false : this.h264plus ? true : this.h265X ? true :
									null
							},
							{
								SmartH264: this.h264 ? true : this.h264plus ? true : this.h265X ? true :
									null
							}
						]
					}]
				};
				if (v == 'Detect.DetectTrack') {
					data = {
						Name: v,
						'Detect.DetectTrack': {
							// Enable: this.TrackingShow ? 1 : 0,
							Enable: this.TrackingShow,
							Sensitivity: this.Sensitivity
						}
					}
				}
				if (v == 'AVEnc.SmartH264') {
					data = {
						Name: v,
						'AVEnc.SmartH264': [{
							SmartH264: !this.imgEnhance
						}]
					}
					console.log(data)
				}
				getApp().globalData.api.setconfig(data, token, (result) => {
					uni.hideLoading();
					if (v == 'Detect.DetectTrack') {
						this.getconfig('Detect.HumanDetection')
					}
				});
			}
		}
	}
</script>

<style>

</style>
